package com.xh.car.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xh.car.bean.Driver;
import com.xh.car.bean.Kaptcha;
import com.xh.car.bean.Userinfo;
import com.xh.car.mapper.DriverMapper;
import com.xh.car.mapper.UserinfoMapper;
import com.xh.car.service.KaptchaService;
import com.xh.car.service.UserinfoService;
import com.xh.car.util.R;
import com.xh.car.util.RTException;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author PangJunjie
 * @Date 2024/5/7/007
 */
@Service
@Transactional
public class UserinfoServiceImpl implements UserinfoService {
    @Autowired
    private KaptchaService kaptchaService;
    @Autowired
    private UserinfoMapper userinfoMapper;
    @Autowired
    private DriverMapper driverMapper;

    @Override
    public Userinfo login(Userinfo userinfo, Kaptcha kaptcha) {
        if (StringUtils.isEmpty(kaptcha.getUuid())) {
            throw new RTException("UUID不能为空");
        }
        if (!kaptchaService.checkKaptcha(kaptcha)) {
            throw new RTException("验证码错误！");
        }
        Userinfo user = userinfoMapper.getByUsername(userinfo.getUsername());
        if (user == null) {
            throw new RTException("用户名或密码不正确！");
        }
        if (!user.getPassword().equals(DigestUtils.md5Hex(userinfo.getPassword()))) {
            throw new RTException("用户名或密码不正确！");
        }
        if (user.getStatus() != 1) {
            throw new RTException("该用户已被封禁，请联系管理员！");
        }
        return user;
    }

    @Override
    public R getPage(int page, int limit, Userinfo userinfo) {
        PageHelper.startPage(page, limit);
        List<Userinfo> list = userinfoMapper.getAll(userinfo);
        PageInfo<Userinfo> pageInfo = new PageInfo<>(list);
        return R.page(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public Userinfo getById(int id) {
        return userinfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Userinfo userinfo, Driver driver) {
        if (userinfo.getId() == null) {
            /*重名验证*/
            Userinfo user = userinfoMapper.getByUsername(userinfo.getUsername());
            if (user != null) {
                throw new RTException("用户名已存在！");
            }
            /*设置密码*/
            userinfo.setPassword(DigestUtils.md5Hex("123456"));
            userinfoMapper.insertSelective(userinfo);
            if (userinfo.getRole() != null && userinfo.getRole() == 4) {
                driver.setUserinfoId(userinfo.getId());
                driverMapper.insertSelective(driver);
            }
        } else {
            if (StringUtils.isNotEmpty(userinfo.getPassword())) {
                userinfo.setPassword(DigestUtils.md5Hex(userinfo.getPassword()));
            }
            userinfoMapper.updateByPrimaryKeySelective(userinfo);
            if (userinfo.getRole() != null && userinfo.getRole() == 4) {
                Driver d = driverMapper.getByUserinfoId(userinfo.getId());
                driver.setId(d.getId());
                driverMapper.updateByPrimaryKeySelective(driver);
            }
        }
    }

    @Override
    public void delete(int id) {
        Driver driver = driverMapper.getByUserinfoId(id);
        if (driver != null) {
            driverMapper.deleteByPrimaryKey(driver.getId());
        }
        userinfoMapper.deleteByPrimaryKey(id);

    }

    @Override
    public R checkPassword(Userinfo userinfo) {
        Userinfo user = userinfoMapper.selectByPrimaryKey(userinfo.getId());
        if (user == null) {
            return R.error("用户编号异常！");
        }
        if (user.getPassword().equals(DigestUtils.md5Hex(userinfo.getPassword()))) {
            return R.ok();
        }
        return R.error("原密码不正确！");
    }

    @Override
    public R getDriverSelect() {
        Userinfo userinfo = new Userinfo();
        userinfo.setRole(4);
        List<Userinfo> list = userinfoMapper.getAll(userinfo);
        return R.data(list);
    }
}
